<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.2</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>-->
    <!--<script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>-->
    <!--<script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>-->
    <!--[endif]-->
    <script type="text/javascript" src="../../common/js/public.js"></script>
    <script type="text/javascript" src="../../common/js/publicUtils.js"></script>

</head>
<body>
<div class="layui-fluid" id="flowDiv">
    <div class="zTreeBackground right" style="padding-left:1%;width:98%">
        <div class="layui-form" style="width:99%;margin-bottom:-10px;text-align:right;">
            <div class="layui-inline" style="text-align:right; !important;">
                <p id="datasourcestate" style="color: red;"></p>
            </div>
            <div class="layui-inline" style="text-align:left; !important;">
                <label class="layui-form-label">表名</label>
                <div class="layui-input-block">
                    <select id="tableName" name="tableName" lay-search></select>
                </div>
            </div>
            <div class="layui-inline" style="text-align:left; !important;">
                <label class="layui-form-label">表描述</label>
                <div class="layui-input-block">
                    <select id="tableComment" name="tableComment" lay-search></select>
                </div>
            </div>
            <button type="button" style="margin-left:12px;margin-top: -12px;" class="layui-btn layui-btn-normal" onclick="moresearch()"><i class="layui-icon layui-icon-search"></i>查询</button>
        </div>
        <table class="layui-hide" id="datatable" lay-filter="datatable"></table>
        <script type="text/html" id="toolbar"></script>

    </div>

</div>
</body>
<SCRIPT type="text/javascript">
    var form,tableObj,table;
    $(function(){
        loadbutton("toolbar");//加载按钮loadbutton(id)，id为button容器id，不传默认为buttongroup
        loadTable();
        loadselect();
        showdatabaseState();
    })


    function loadselect(){
        layui.use(['form','element'],function() {
            var datasource = store.get("datasource");
            if(!datasource){
                return false;
            }
            datasource["pageIndex"] = 1;
            datasource["pageSize"] = 10000000;
            form = layui.form;
            publicAjax(null,'tool/gen/jdbclist',datasource,null,null,function(r){
                var data = r.data.list;
                var nameoption = "<option value=''>请选择表名称</option>";
                var commentoption = "<option value=''>请选择表描述</option>";
                $.each(data,function(i,e){
                    nameoption += "<option value='"+e.tableName+"'>"+e.tableName+"</option>";
                    if(e.tableComment){
                        commentoption += "<option value='"+e.tableComment+"'>"+e.tableComment+"</option>";
                    }
                })
                $("#tableName").html(nameoption);
                $("#tableComment").html(commentoption);
                form.render();
            })
        })

    }
    function moresearch(){
        var tableName = $("#tableName").val();
        var tableComment = $("#tableComment").val();
        loadTable(tableName,tableComment);
    }
    /* *
     * @Author bxf
     * @Description 初始化table
     * @Date 17:51 2019/8/30
     * @param null
     * @return
     */
    function loadTable(tableName,tableComment){
        var datasource = store.get("datasource");
        if(tableName){
            datasource["tableName"] = tableName;
        }
        if(tableComment){
            datasource["tableComment"] = tableComment;
        }
        var tableurl = publicUrl+'tool/gen/jdbclist';
        layui.use(['form','table','element'], function(){
            var element = layui.element;
            form = layui.form;
            table = layui.table;
            tableObj = table.render({
                elem: '#datatable',
                size: 'sm',
                toolbar: '#toolbar',
                even: true, //开启隔行背景
                height: 'full-100',
                method: 'post',
                headers: {"Authorization":getData("Authorization")},
                url: tableurl, //stationId='+window.parent.window.parentId,//8AE7DECA-8E41-4FAB-8B30-9FD1B0B7FA33,78A9E5D8-20FB-4436-BBE8-A25EAC38D056
                where:datasource,
                response: {
                    statusCode: 2000,
                },
                parseData: function(res){ //res 即为原始返回的数据
                    return formatTableParseData(res);
                },
                request: {
                    pageName: 'pageIndex' //页码的参数名称，默认：page
                    ,limitName: 'pageSize' //每页数据量的参数名，默认：limit
                },
                cols: [[
                    {
                        type:'checkbox',
                        width: 60,
                    },
                    {
                        type: 'numbers',
                        title: '序号',
                        width: 80,
                        align: 'center',
                    },{
                        field: 'tableName',
                        title: '表名',
                        sort: true,
                        width: "15%",
                        align: 'center',
                    }, {
                        field: 'tableComment',
                        title: '表描述',
                        sort: true,
                        width: "20%",
                        align: 'center'
                    },{
                        field: 'className',
                        title: '实体类名称',
                        sort: true,
                        width: "20%",
                        align: 'center'
                    },{
                        field: 'createTime',
                        title: '创建时间',
                        width: "15%",
                        align: 'center',
                    },{
                        field: 'updateTime',
                        title: '更新时间',
                        sort: true,
                        width: "15%",
                        align: 'center'
                    }]],
                page: true
            });
            element.render();
        });
    }


    function uploadGettable(){
        var datasource = store.get("datasource");
        if(!datasource){
            layer.alert("请先设置数据源！");
            return false;
        }
        var iframeWin ;
        layer.open({
            type: 2,
            area: ["1200px" , "600px"],
            fix: false,
            //不固定
            btn: ['确定','取消'],
            shade: 0.3,
            title: "导入表结构",
            content: "./uploadgenset.html",
            // 弹层外区域关闭
            shadeClose: false,
            yes: false,
            cancel: function(index) {
                loadTable();
                loadselect();
                return true;
            },
            yes: function(index, layero) {
                var resultdata = iframeWin.contentWindow.importanttable();//应用
                if(resultdata && resultdata.code==2000){
                    loadTable();
                    loadselect();
                    delayLoad(function(){
                        layer.closeAll();
                    })
                }else{
                    return false;
                }
            },
            success: function(layero, index){
                iframeWin = layero.find('iframe')[0];
            }
        });
    }

    // function uploadGettable(){
    //     var iframeWin ;
    //     publicAjax(null,"tool/gen/testConnect",data,false,null,function (r) {
    //         if(r.code==2000){
    //             layer.open({
    //                 type: 2,
    //                 area: ["1200px" , "600px"],
    //                 fix: false,
    //                 //不固定
    //                 btn: ['连接测试','确定','取消'],
    //                 shade: 0.3,
    //                 title: "导入表结构",
    //                 content: "./uploadgenset.html",
    //                 // 弹层外区域关闭
    //                 shadeClose: false,
    //                 yes: false,
    //                 cancel: function(index) {
    //                     moresearch();
    //                     return true;
    //                 },
    //                 yes: function(index, layero) {
    //                     iframeWin.contentWindow.testconnect();//应用
    //                 },
    //                 btn2: function(index, layero){
    //                     var resultdata = iframeWin.contentWindow.datasourceset();//添加
    //                     if(resultdata && resultdata.code==2000){
    //                         showdatabaseState();
    //                         delayLoad(function(){
    //                             layer.closeAll();
    //                         },500)
    //                     }else{
    //                         return false;
    //                     }
    //                     //return false 开启该代码可禁止点击该按钮关闭
    //                 },
    //                 btn3: function(index, layero){
    //                     //按钮【按钮三】的回调
    //
    //                     //return false 开启该代码可禁止点击该按钮关闭
    //                 },
    //                 success: function(layero, index){
    //                     iframeWin = layero.find('iframe')[0];
    //                 }
    //             });
    //         }else{
    //             layer.alert("连接失败，请输入正确账号密码！")
    //         }
    //     })
    // }
    function showdatabaseState(){
        var detaildata = store.get("detaildata");
        var datasource = store.get("datasource");
        if(!datasource){
            var html = "数据库未连接，请配置数据源！";
        }else{
            publicAjax(null,"tool/gen/testConnect",datasource,null,null,function (r) {
                if(r.code==2000){
                    var datasourceTypeHtml = detaildata.datasourceType==3?'sqlserver数据库连接成功！':(detaildata.datasourceType==2?'oracle数据库连接成功！':'mysql数据库连接成功！');
                    var html = '服务器'+detaildata.jdbcurl+'的'+datasourceTypeHtml;
                }else{
                    var html = "数据库未连接，请检查后重新配置数据源！";
                }
                $("#datasourcestate").html(html);
            })

        }

    }

    function datasourceSet(){
        var iframeWin ;
        layer.open({
            type: 2,
            area: ["500px" , "500px"],
            fix: false,
            //不固定
            btn: ['连接测试','确定','取消'],
            shade: 0.3,
            title: "数据源设置",
            content: "./datasource.html",
            // 弹层外区域关闭
            shadeClose: false,
            cancel: function(index) {
                loadTable();
                loadselect();
                return true;
            },
            yes: function(index, layero) {
                iframeWin.contentWindow.testconnect();//测试
            },
            btn2: function(index, layero){
                var resultdata = iframeWin.contentWindow.datasourceset();//添加
                if(resultdata && resultdata.code==2000){
                    loadTable();
                    loadselect();
                    showdatabaseState();
                    delayLoad(function(){
                        layer.closeAll();
                    },500)
                }else{
                    return false;
                }
            },
            success: function(layero, index){
                iframeWin = layero.find('iframe')[0];
            }
        });
    }

    function gencode(){
        var obj = table.checkStatus('datatable');
        if(!obj || obj.data.length!=1){
            layer.alert("请选择一项信息进行修改！");
            return false;
        }
        var data = obj.data[0]; //idTest 即为基础参数 id 对应的值
        parent.window.location.href=publicUrl+"/tool/gen/common/download/genCode/"+data.tableId;
    }
    /**
     * @Author bxf
     * @Description 添加工艺工法
     * @Date 9:51 2019/7/18
     * @Param
     * @return
     **/
    function genedit(){
        var obj = table.checkStatus('datatable');
        if(!obj || obj.data.length!=1){
            layer.alert("请选择一项信息进行修改！");
            return false;
        }
        var data = obj.data[0]; //idTest 即为基础参数 id 对应的值
        var iframeWin ;
        var index = layer.open({
            type: 2,
            area: ["1200px" , "600px"],
            fix: false,
            //不固定
            shade: 0.3,
            title: "编辑列名",
            content: "./genedit.html",
            // 弹层外区域关闭
            shadeClose: false,
            success: function(layero, index){
                iframeWin = layero.find('iframe')[0];
                iframeWin.contentWindow.initcolumns(data.tableId);//应用
            }
        });
        layer.full(index);
    }

    /* *
     * @Author bxf
     * @Description 删除记录
     * @Date 11:19 2019/7/23
     * @param null
     * @return 
     */
    function  deleteTable() {
        //获取选中项
        var obj = table.checkStatus('datatable');
        if(!obj || obj.data.length<1){
            layer.alert("请至少选择一项信息！");
            return false;
        }
        var ids = new Array();
        for(var i=0;i<obj.data.length;i++){
            ids.push(obj.data[i].tableId);
        }
        layui.use(['layer'],function(){
            var layer = layui.layer;
            layer.confirm('确定删除？', {
                btn: ['确定', '取消'] //可以无限个按钮
            }, function(){
                var delurl = 'tool/gen/remove';
                var data = {"ids":ids.join(",")};//删除
                publicAjax (null,delurl,data,null,null,function (r) {
                    if(r.code==2000){
                        laysuccess("删除成功",null);
                        moresearch();
                    }else{
                        layerror("删除失败",null);
                    }
                });
            }, function(){
                layer.closeAll();

            });
        })
    }


</script>
</html>